#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
    1. 读取配置文件时，都是字符串格式的
    2. 有些本身是int或bool类型的，放在这个类里直接解析成相应格式
    3. 对于路径，解析成字符串
"""

import time
import pathlib
import configparser
from myinit import args

class Singleton(object):
    def __init__(self, cls):
        self._cls = cls
        self._instance = {}
    def __call__(self,*args):
        if self._cls not in self._instance:
            self._instance[self._cls] = self._cls(*args)
        return self._instance[self._cls]

@Singleton
class Configure(object):
    def __init__(self, config_dir):
        config = configparser.ConfigParser(allow_no_value=True)
        # 读取配置文件
        config.read(str(config_dir.joinpath('server.ini').resolve()))

        self.BeginYear = config.get('server', 'BeginYear')
        self.BeginMonth = config.get('server', 'BeginMonth')
        self.BeginDay = config.get('server', 'BeginDay').zfill(2)
        self.BeginHour = config.get('server', 'BeginHour').zfill(2)
        self.EndYear = config.get('server', 'EndYear')
        self.EndMonth = config.get('server', 'EndMonth')
        self.EndDay = config.get('server', 'EndDay').zfill(2)
        self.EndHour = config.get('server', 'EndHour').zfill(2)
        nowYear = time.strftime("%Y", time.localtime())
        nowMonth = time.strftime("%m", time.localtime())
        nowDay = time.strftime("%d", time.localtime()).zfill(2)
        nowHour = time.strftime("%H", time.localtime()).zfill(2)
        self.EndYear = nowYear if self.EndYear == '-1' else self.EndYear
        self.EndMonth = nowMonth if self.EndMonth == '-1' else self.EndMonth
        self.EndDay = nowDay if self.EndDay == '-1' else self.EndDay
        self.EndHour = nowHour if self.EndHour == '-1' else self.EndHour

        self.BeginTime = "'" + self.BeginYear + self.BeginMonth + self.BeginDay + self.BeginHour + "'"
        self.EndTime = "'" + self.EndYear + self.EndMonth + self.EndDay + self.EndHour + "'"
        self.LoggingConfPath = str(pathlib.Path(config.get('server', 'LoggingConfPath')).absolute())
        self.Parquet = config.get('server', 'Parquet')


        # model
        self.CVModel = str(pathlib.Path(config.get('model', 'CVModel')).absolute())
        self.IDFModel = str(pathlib.Path(config.get('model', 'IDFModel')).absolute())
        self.JieBa = str(pathlib.Path(config.get('model', 'JieBa')).absolute())
        self.TopK = config.getint('model', 'TopK')
        self.TRWindow = config.getint('model', 'TRWindow')
        self.TRWordMinLen = config.getint('model', 'TRWordMinLen')

        # mysql
        self.MysqlHost = config.get('mysql', 'host')
        self.MysqlPort = config.get('mysql', 'port')
        self.MysqlUser = config.get('mysql', 'user')
        self.MysqlPassword = config.get('mysql', 'password')
        self.MysqlDatabase = config.get('mysql', 'database')

config = Configure(args.conf)